"""
用户反馈模型
"""
from sqlalchemy import Column, String, DateTime, Text, ForeignKey, Enum as SQLEnum
from sqlalchemy.sql import func
from app.core.database import Base
import enum


class FeedbackType(enum.Enum):
    """反馈类型"""
    ERROR = "error"               # 信息错误
    SUGGESTION = "suggestion"     # 建议修改
    COMPLAINT = "complaint"       # 投诉
    NEW_VENUE = "new_venue"      # 建议新场地


class FeedbackStatus(enum.Enum):
    """反馈状态"""
    PENDING = "pending"           # 待处理
    PROCESSING = "processing"     # 处理中
    RESOLVED = "resolved"         # 已解决
    IGNORED = "ignored"          # 已忽略


class VenueFeedback(Base):
    """用户反馈表"""
    __tablename__ = "venue_feedbacks"
    
    id = Column(String(36), primary_key=True)
    venue_id = Column(String(36), ForeignKey('venues.id', ondelete='SET NULL'), comment="关联场地ID")
    user_id = Column(String(36), ForeignKey('users.id'), nullable=False, comment="反馈用户ID")
    
    feedback_type = Column(SQLEnum(FeedbackType), nullable=False, comment="反馈类型")
    content = Column(Text, nullable=False, comment="反馈内容")
    contact_phone = Column(String(20), comment="联系电话")
    
    status = Column(SQLEnum(FeedbackStatus), default=FeedbackStatus.PENDING, comment="处理状态")
    handled_by = Column(String(36), comment="处理人ID")
    handled_at = Column(DateTime, comment="处理时间")
    handler_note = Column(Text, comment="处理备注")
    
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "venueId": self.venue_id,
            "userId": self.user_id,
            "feedbackType": self.feedback_type.value if isinstance(self.feedback_type, enum.Enum) else self.feedback_type,
            "content": self.content,
            "contactPhone": self.contact_phone,
            "status": self.status.value if isinstance(self.status, enum.Enum) else self.status,
            "handledBy": self.handled_by,
            "handledAt": self.handled_at.isoformat() if self.handled_at else None,
            "handlerNote": self.handler_note,
            "createdAt": self.created_at.isoformat() if self.created_at else None,
            "updatedAt": self.updated_at.isoformat() if self.updated_at else None,
        }

